package com.adwin.addcoin.ui.dialog

import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup.LayoutParams
import android.widget.Toast
import com.adwin.addcoin.base.BaseDialog
import com.adwin.addcoin.databinding.DialogInvitationCodeLayoutBinding
import com.adwin.addcoin.repository.SchedulerTransformer
import com.adwin.addcoin.repository.api.TestApi
import com.adwin.addcoin.repository.net.RetrofitClient
import com.adwin.addcoin.utils.FlowKeyUtils
import com.adwin.addcoin.utils.PersonalMessageUtils
import com.adwin.addcoin.utils.installRepost
import com.cashbird.app.infra.utils.FlowBus
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import org.json.JSONObject


class InvitationCodeDialog(private val mContext:Context) : BaseDialog(mContext){

    lateinit var  binding:DialogInvitationCodeLayoutBinding

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

    }

    override fun getGravity(): Int = Gravity.CENTER

    override fun getWidth(): Int =LayoutParams.MATCH_PARENT

    override fun getHeight(): Int = LayoutParams.WRAP_CONTENT

    override fun initLayout(): View {

        binding = DialogInvitationCodeLayoutBinding.inflate(LayoutInflater.from(mContext))
        return binding.root
    }

    override fun onStart() {
        super.onStart()

    }

    override fun initData() {
        GlobalScope.launch(Dispatchers.IO) {
            context.installRepost("InvitationDialogShow")
        }
        setOnClickListener()
    }

    private fun sendInvitaionCode(){
        val code = binding.etInvitationCode.text.toString()
        if (TextUtils.isEmpty(code)) return
        binding.etInvitationCode.setText("")
        MainScope().launch (Dispatchers.IO){
            val json = JSONObject()
            json.put("invitationCode",code)
            mContext.installRepost("sendInvitaionCode",json)
        }
        RetrofitClient.getInstance().getService(TestApi::class.java).editInviteCode(code).compose(
            SchedulerTransformer()
        ).subscribe({
            if (it.isSuccessful){
                if (it.body()?.code == 200){
                    Toast.makeText(context,it.body()?.msg?:"",Toast.LENGTH_SHORT).show()
                    MainScope().launch (Dispatchers.IO){
                        val json = JSONObject()
                        json.put("bindCode",code)
                        mContext.installRepost("InvitaionCodeSuccess",json)
                    }
                    MainScope().launch(Dispatchers.IO) {
                        FlowBus.with<String>(FlowKeyUtils.CoinsChange).post("")
                    }
                    dismiss()
                }else{
                    Toast.makeText(context,it.body()?.msg?:"",Toast.LENGTH_SHORT).show()
                }
            }else{
                Toast.makeText(context,it.message(),Toast.LENGTH_SHORT).show()
            }
        },{

        })
    }

    private fun setOnClickListener(){
        binding.tvInvitationCodeConfirm.setOnClickListener {
            sendInvitaionCode()
        }
    }

}